import numpy as np
import pandas as pd
import re, os
import colorama
import warnings
warnings.filterwarnings("ignore")
colorama.init(autoreset=True)
TEXT_COLOR_RED = colorama.Fore.RED
TEXT_COLOR_GREEN = colorama.Fore.GREEN
TEXT_COLOR_YELLOW = colorama.Fore.YELLOW
TEXT_COLOR_END = colorama.Fore.RESET
root_path=os.path.join(os.getcwd(),".\\Data_process\\Filter")
def fout(input_file_name,process_name,out_format):
    info_list=input_file_name.split(".")[0].split("-")[0].split("_")
    fout_prefix = f'{process_name}_'+"_".join(info_list[1:])
    ntry = 0
    while True:
        if ntry == 0:
            fout = fout_prefix + f'.{out_format}'
        else:
            fout = fout_prefix + '-%d.%s' % (ntry,out_format)
        if os.access(os.path.join(root_path,fout), os.F_OK):
            ntry += 1
        else:
            break
    return fout
def main(inp_df):
    print('Columns : %s' % (' '.join(inp_df.columns, )))
    inp_reserve = input('If you want to ignore some variables, enter the variables to be reserved: \n').strip()
    if len(inp_reserve) != 0:
        inp_ce = input('Enter the variable corresponding to CE: \n').strip()
    else:
        inp_ce = ''
    if len(inp_ce) != 0 and len(inp_reserve) != 0:
        var_keys = inp_reserve.split()
        ce_key = inp_ce
    check_bool='1'
    while check_bool!='0':
        nfilter = 1
        filtered  = inp_df
        filter_df_before=inp_df
        key_before=""
        while True:
            filter_input = input('Filter %d: ' % (nfilter,)).strip()
            if len(filter_input) == 0:
                break
            filter_cond = filter_input.split()
            key, floor, ceil = filter_cond[0], float(filter_cond[1]), float(filter_cond[2])
            if key==key_before:
                filter=filter_df_before.copy()
                filter_bool=((filter[key] >= floor) & (filter[key] <= ceil))
                data_tmp = filter[filter_bool]
                if len(inp_ce) != 0 and len(inp_reserve) != 0:
                    drop_duplicated_data = data_tmp.sort_values(by=ce_key).drop_duplicates(subset=var_keys, keep='last')
                    drop_duplicated_data.to_csv(os.path.join(root_path, f"Filter_{nfilter}.csv"), index=False)
                    print(f"Please Check: Filter_{nfilter}.csv Total Columns: {len(drop_duplicated_data)}")
                    filtered=pd.concat([filtered,drop_duplicated_data])
                else:
                    data_tmp.to_csv(os.path.join(root_path, f"Filter_{nfilter}.csv"), index=False)
                    print(f"Please Check: Filter_{nfilter}.csv Total Columns: {len(data_tmp)}")
                    filtered=pd.concat([filtered,data_tmp])

            else:
                filter=filtered.copy()
                filter_bool=((filter[key] >= floor) & (filter[key] <= ceil))
                data_tmp = filter[filter_bool]
                if len(inp_ce) != 0 and len(inp_reserve) != 0:
                    drop_duplicated_data = data_tmp.sort_values(by=ce_key).drop_duplicates(subset=var_keys, keep='last')
                    drop_duplicated_data.to_csv(os.path.join(root_path, f"Filter_{nfilter}.csv"), index=False)
                    print(f"Please Check: Filter_{nfilter}.csv Total Columns: {len(drop_duplicated_data)}")
                    filtered=drop_duplicated_data.copy()
                else:
                    data_tmp.to_csv(os.path.join(root_path, f"Filter_{nfilter}.csv"), index=False)
                    print(f"Please Check: Filter_{nfilter}.csv Total Columns: {len(data_tmp)}")
                    filtered=data_tmp.copy()

            key_before,filter_df_before=key,filter.copy()
            nfilter = nfilter + 1
        print(f"{nfilter-1} Filter conditions are involed.")
        print('Total rows:' + TEXT_COLOR_GREEN + ' %d' % (len(filtered)) + TEXT_COLOR_END)
        print('Total Columns:'+ TEXT_COLOR_GREEN + ' %d  column names:%s' % (len(filtered.columns),filtered.columns.tolist()) + TEXT_COLOR_END)
        check_bool = input('Plesse Check Your Filtered Results ,Then  Enter [0] to stop Filter or Enter [1] to ReFilter: \n').strip().split()[0]
    return filtered


if __name__ == '__main__':
    root_path=os.getcwd()
    finp = input('Enter raw data file coresponding to Setfos_R_Ref.csv or Setfos_R_5.csv :\n').strip()
    inp_df = pd.read_csv(os.path.join(root_path, finp))
    check_bool = '1'
    filtered=main(inp_df)
    fout=fout(finp,f'Filter',"csv")
    filtered.to_csv(os.path.join(root_path, fout), index=False)
    print('Total rows:' + TEXT_COLOR_GREEN + ' %d' % (len(filtered)) + TEXT_COLOR_END)
    print('Write output file ' + TEXT_COLOR_GREEN + "%s" % (fout,) + TEXT_COLOR_END + ' successfully!')

#CIEx_0 0.682 0.6835
#CIEx_0 0.684 0.6855